//
//  BJYDownloadVC.m
//  JW_BasePorject
//
//  Created by JW on 2018/6/29.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "BJYDownloadVC.h"

//#import <BJPlayerManagerCore/BJPlayerManagerCore.h>

#import "PMDownloadModel+ZYVideo.h"
#import "CacheGroupModel.h"
#import "BJYFolderCell.h"

#import "DownloadChapterVC.h"

@interface BJYDownloadVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *cacheTableView;

@property (nonatomic, strong)NSMutableArray *videoList;

@end

@implementation BJYDownloadVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.cacheTableView];
        
    [self initDownloadData];
}


- (void)initDownloadData{
    
    NSMutableArray *downloadingList = [NSMutableArray array];
    if([[PMDownloadManager downloadManager].downloadingList count]) {
        downloadingList = [NSMutableArray arrayWithArray:[PMDownloadManager downloadManager].downloadingList];
    }
    
    //正在缓存
    NSDictionary *downloadingDic = @{@"showName": @"正在缓存",
                                     @"cacheState": @(ZYDownloading),
                                     @"videoList" : downloadingList
                                     };
    
    [self.videoList addObject:[CacheGroupModel mj_objectWithKeyValues:downloadingDic]];
    MJExtensionLog(@"正在下载:%@", [CacheGroupModel mj_objectWithKeyValues:downloadingDic]);

        
    NSMutableArray *finishedList = [NSMutableArray array];
    if([[PMDownloadManager downloadManager].finishedList count]) {
        finishedList = [NSMutableArray arrayWithArray:[PMDownloadManager downloadManager].finishedList];
    }
    
    NSMutableSet *set = [NSMutableSet set];
    [finishedList enumerateObjectsUsingBlock:^(PMDownloadModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *course_name = obj.course_name;
        [set addObject:course_name];
    }];
    
    __block NSMutableArray *groupArr = [NSMutableArray array];
    [set enumerateObjectsUsingBlock:^(NSString * _Nonnull str, BOOL * _Nonnull stop) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"course_name = %@", str];
        NSArray *tempArr = [NSArray arrayWithArray:[finishedList filteredArrayUsingPredicate:predicate]];
        NSDictionary *downloadedDic = @{ @"showName": str,
                                         @"cacheState": @(ZYDownloaded),
                                         @"videoList" : tempArr
                                         };
        [groupArr addObject:[CacheGroupModel mj_objectWithKeyValues:downloadedDic]];
        MJExtensionLog(@"已下载:%@", [CacheGroupModel mj_objectWithKeyValues:downloadedDic]);
//        NSDictionary *dic = @{str: tempArr};
//        [groupArr addObject:dic];
    }];
    
    //已缓存
    [self.videoList addObjectsFromArray:groupArr];
    
    [self.cacheTableView reloadData];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.videoList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BJYFolderCell *cell = [BJYFolderCell cellWithTableView:tableView];
    cell.groupModel = self.videoList[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CacheGroupModel *group = self.videoList[indexPath.row];
    DownloadChapterVC *chapterVC = [[DownloadChapterVC alloc] init];
    chapterVC.courseName = group.showName;
    [self.navigationController pushViewController:chapterVC animated:YES];

}


- (UITableView *)cacheTableView{
    if (_cacheTableView == nil) {
        _cacheTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBarHeight-36) style:UITableViewStylePlain];
        _cacheTableView.delegate = self;
        _cacheTableView.dataSource = self;
        [_cacheTableView registerClass:[BJYFolderCell class] forCellReuseIdentifier:@"BJYFolderCell"];
        _cacheTableView.rowHeight = UITableViewAutomaticDimension;
        // 设置tableView的估算高度
        _cacheTableView.estimatedRowHeight = 60;
        _cacheTableView.tableFooterView = [UIView new];
        
    }
    return _cacheTableView;
}

- (NSMutableArray *)videoList{
    if (_videoList == nil) {
        _videoList = [NSMutableArray array];
    }
    return _videoList;
}

@end
